package com.q.tqb.activity.grab;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.MainActivity;
import com.q.tqb.activity.ViewPagerImageActivity;
import com.q.tqb.adapter.MineSendRedAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.MineRedInfo;
import com.q.tqb.model.UserInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.MD5Util;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.CircleImageView;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class RedPersonDataActivity extends BaseActivity {


    @BindView(R.id.iv_head)
    CircleImageView mIvHead;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_tel)
    TextView mTvTel;
    @BindView(R.id.tv_tro)
    TextView mTvTro;
    @BindView(R.id.ll_tel)
    LinearLayout mLiTel;
    @BindView(R.id.rv_send_red)
    GRecyclerView mRvSendRed;
    private int uid;

    private MineSendRedAdapter mMineRedAdapter;
    private ArrayList<MineRedInfo> mGrabRedDatas;

    private int currentPageNo;
    private UserInfo personData;
    private String[] mImages;

    @Override
    public int getLayoutId() {
        return R.layout.activity_red_person_data;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("他(她)的资料");
        uid = getIntent().getIntExtra("uid", 0);
        mImages = new String[1];
        getUserInfo(uid);
    }


    private void getUserInfo(int uid) {
        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("uid", uid + "");
        hashMap.put("token", StringUtil.getToken(uid));
        OkHttpUtil.getInstance().post(this, AppConst.URL_GET_USER_INFO, hashMap, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                Gson gson = new Gson();
                String result = element.toString().replace("null", "\"\"");
                personData = gson.fromJson(result, UserInfo.class);

                if (personData.getIs_show_privacy() == 1) {
                    mTvTel.setText(personData.getMobile());
                    mLiTel.setVisibility(View.VISIBLE);
                }

                mImages[0] = personData.getHeader();

                Glide
                        .with(RedPersonDataActivity.this)
                        .load(personData.getHeader())
                        .error(R.drawable.ic_head)
                        .placeholder(R.drawable.ic_head)
                        .dontAnimate()
                        .into(mIvHead);

                mTvName.setText(personData.getNickname());

                mTvTro.setText(personData.getSignature());

                init(personData.getHeader());

            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg);
            }
        });
    }

    @OnClick({R.id.iv_back, R.id.iv_head})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_head:
                if (personData != null) {
                    Intent intent = new Intent(RedPersonDataActivity.this, ViewPagerImageActivity.class);
                    intent.putExtra("position", 0);
                    intent.putExtra("images", mImages);
                    startActivity(intent);
                }
                break;
            default:
                break;
        }
    }

    private void init(String head) {
        mGrabRedDatas = new ArrayList<>();

        mRvSendRed.setLoadingMoreEnabled(true);
        mRvSendRed.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPageNo = 1;
                mRvSendRed.setNoMore(false);
                mRvSendRed.setLoadingMoreEnabled(true);
                getListData();
            }

            @Override
            public void onLoadMore() {
                currentPageNo++;
                getListData();
            }
        });

        mMineRedAdapter = new MineSendRedAdapter(RedPersonDataActivity.this, mGrabRedDatas, 2, head);

        mMineRedAdapter.setOnItemClickListener(new MineSendRedAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent;
                if (mGrabRedDatas.get(position).getPacket_type() == 2) {
                    intent = new Intent(RedPersonDataActivity.this, GrabSpeaking2Activity.class);
                    intent.putExtra("rid", mGrabRedDatas.get(position).getRid());
                    startActivity(intent);
                } else if (mGrabRedDatas.get(position).getPacket_type() == 1) {
                    intent = new Intent(RedPersonDataActivity.this, GrabSpeaking2Activity.class);
                    intent.putExtra("rid", mGrabRedDatas.get(position).getRid());
                    startActivity(intent);
                } else {
                    showToast("该红包为地图红包，请到地图上查看！");
                }
            }

            @Override
            public void onLongClick(final int position) {
            }
        });

        LinearLayoutManager linearLayoutManagert = new LinearLayoutManager(RedPersonDataActivity.this);
        mRvSendRed.setLayoutManager(linearLayoutManagert);
        mRvSendRed.setAdapter(mMineRedAdapter);
        mRvSendRed.setRefresh();
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", currentPageNo + "");
        map.put("pageSize ", 10 + "");
        map.put("uid", uid + "");
        OkHttpUtil.getInstance().post(RedPersonDataActivity.this, AppConst.URL_MINE_POST_PACKET_LIST, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<MineRedInfo> grabRedDatas = gson.fromJson(element, new TypeToken<ArrayList<MineRedInfo>>() {
                }.getType());
                if (currentPageNo == 1) {
                    mGrabRedDatas.clear();
                    mRvSendRed.refreshComplete();
                    if (grabRedDatas.size() < 10) {
                        mRvSendRed.setNoMore(true);
                    }
                } else {
                    if (grabRedDatas.size() < 10) {
                        mRvSendRed.setNoMore(true);
                    } else {
                        mRvSendRed.loadMoreComplete();
                    }
                }
                mGrabRedDatas.addAll(grabRedDatas);
                mMineRedAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                if (currentPageNo == 1) {
                    mRvSendRed.refreshComplete();
                } else {
                    mRvSendRed.loadMoreComplete();
                }
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                Toast.makeText(RedPersonDataActivity.this, errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

